package com.lagou.controller;

import com.lagou.IpUtil;
import com.lagou.bean.ConsumerComponent;
import org.apache.dubbo.rpc.RpcContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Comparator;
import java.util.TreeMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Controller
public class WebController {

    @Resource
    private ConsumerComponent consumerComponent;

    @GetMapping("/hello")
    @ResponseBody
    public String sayHello(@RequestParam(value = "name") String name, HttpServletRequest request) {

        return consumerComponent.sayHello(name, 4);
    }

    @GetMapping("/world")
    @ResponseBody
    public String sayWorld( @RequestParam(value = "name") String name,HttpServletRequest request) {
        return consumerComponent.sayWorld(name, 4);
    }

    @GetMapping("/tp")
    @ResponseBody
    public void tp( HttpServletRequest request) {
        System.out.println("tp开始==========");



        ExecutorService service = Executors.newCachedThreadPool();
        service.submit(new Runnable() {
            @Override
            public void run() {
                 for(int i= 0;i<2000;i++) {
                     consumerComponent.methodA();
                 }
            }
        });

        service.submit(new Runnable() {
            @Override
            public void run() {
                for(int i= 0;i<2000;i++) {
                    consumerComponent.methodB();
                }
            }
        });

        service.submit(new Runnable() {
            @Override
            public void run() {
                for(int i= 0;i<2000;i++) {
                    consumerComponent.methodC();
                }
            }
        });
    }

}
